package com.cerosoft.components;

import java.net.URLEncoder;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

import com.cerosoft.api.Config;
import com.cerosoft.utils.IndexChangeListener;

public class DynamicVideoView extends VideoView implements IndexChangeListener {

	private List<String> videos = null;

	private int currIndex = -1;

	private String ipAddr = null;

	private void getViewByIndex() {
		if (videos == null) {
			return;
		}
		String currPath = videos.get(currIndex);
		if (isPlaying()) {
			stopPlayback();
		}
		setVideoPath(URLEncoder.encode(ipAddr + Config.DOWNLOAD_URL_PART
				+ currPath));
		start();
	}

	public void initView(List<String> videos, String ipAddr, int currIndex) {
		this.videos = videos;
		this.ipAddr = ipAddr;
		this.currIndex = currIndex;
		getViewByIndex();
	}

	public DynamicVideoView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public int indexAdd() {
		if (videos != null && currIndex < videos.size() - 1) {
			currIndex++;
			getViewByIndex();
		}
		return 0;
	}

	@Override
	public int indexSub() {
		if (videos != null && currIndex > 0) {
			currIndex--;
			getViewByIndex();
		}
		return 0;
	}

}
